package com.jiangjiang.eshop.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jiangjiang.eshop.common.help.DateHelper;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;

/**
 * @ClassName MyMetaObjectHandler
 * @Description 处理mybatis-plus自动填充创建时间和更新时间
 * @Author jhw
 * @Date 2021/4/20 23:23
 * @Version 1.0
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private DateHelper dateHelper;

    @SneakyThrows
    @Override
    public void insertFill(MetaObject metaObject) {
        Date currentDate = dateHelper.getCurrentDate();
        this.setFieldValByName("gmtCreate", currentDate, metaObject);
        this.setFieldValByName("gmtModified", currentDate, metaObject);
    }

    @SneakyThrows
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmtModified", dateHelper.getCurrentDate(), metaObject);
    }
}
